package com.dsx.myblog.web;

import com.dsx.myblog.entity.Article;
import com.dsx.myblog.service.ArticleService;
import com.dsx.myblog.util.Markdown2HtmlUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

/**
 * @author dsx
 */
@Controller
@RequestMapping(value="/articles/")
@Log
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @ResponseBody
    @ApiOperation(value="获取文章", notes="")
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public Map<String, Object> getArticle(@PathVariable int id) {
        Map<String, Object> map = new HashMap<>();
        Article article = articleService.get(id);
        String s = Markdown2HtmlUtil.markdown2html(article.getContent());
        map.put("bean", article);
        map.put("markDownHtml", s);
        return map;
    }

    @ResponseBody
    @ApiOperation(value="分页获取文章列表", notes="分页获取文章")
    @RequestMapping(value = "{pageNum}/{pageSize}", method = RequestMethod.GET)
    public PageInfo getArticleListByPage(@PathVariable int pageNum, @PathVariable int pageSize) {
        List<Article> articles = articleService.list(pageNum, pageSize);
        return new PageInfo(articles);
    }
    @ResponseBody
    @ApiOperation(value="添加文章", notes="")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public void postArticle(@RequestBody Article article) {
        article.setGmt_create(new Date());
        article.setGmt_modified(new Date());
        System.out.println("_-_" + article);
        articleService.add(article);
    }




//    @ResponseBody
//    @ApiOperation(value="根据id获取文章", notes="")
//    test git 2 @RequestMapping(value = "{id}", method = RequestMethod.GET)
//    public Article getArticle(@PathVariable Long id) {
//        return articleService.get(id);
//    }

    @ResponseBody
    @ApiOperation(value="更新文章", notes="")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public void putArticle(@RequestBody Article article) {

        System.out.println("_-_ " + article);

        articleService.update(article);
    }

    @ResponseBody
    @ApiOperation(value="删除文章", notes="test")
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public void deleteArticle(@PathVariable Long id) {
        articleService.delete(id);
    }



    @Value("${file.install.package.android}")
    String androidInstallPackagePath;

    @Value("${file.install.package.ios}")
    String iosInstallPackagePath;

    @Value("${file.install.package.windows}")
    String windowsInstallPackagePath;

    @RequestMapping("test/{type}")
    public void test(HttpServletResponse response , @PathVariable String type){
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        String installPackagePath;
        switch (type){
            case "android": installPackagePath = androidInstallPackagePath; break;
            case "ios": installPackagePath = iosInstallPackagePath; break;
            case "windows": installPackagePath = windowsInstallPackagePath; break;
            default: log.info("请求下载安装包类型错误");return;
        }

        try{
            String[] fileNames  = ResourceUtils.getFile(installPackagePath).list();
            assert fileNames != null;
            Arrays.sort(fileNames);
            String fileName = fileNames[fileNames.length-1];
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            File file = ResourceUtils.getFile(installPackagePath + fileName);
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            OutputStream os = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer);
                i = bis.read(buffer);
            }
            bis.close();
            os.close();
        }  catch (IOException e) {
            log.info("安装包下载失败");
            e.printStackTrace();
        }
    }


    @ResponseBody
    @ApiOperation(value="测试", notes="")
    @RequestMapping(value = "test/transactional", method = RequestMethod.GET)
    public Void testTransactional() {
        articleService.testTransactional();
        return null;
    }




}
